#include "jsonUtils.h"
#include <rapidjson/writer.h>

void JsonUtils::loadStr(const JsonValue& v, const Char* name, String& str)
{
	const JsonValue::ConstMemberIterator& it = v.FindMember(name);
	if (it != v.MemberEnd() && it->value.IsString()) {
		str = it->value.GetString();
	}
}

void JsonUtils::loadStrArr(const JsonValue& v, const Char* name, std::list<String>& strs)
{
	const JsonValue::ConstMemberIterator& it = v.FindMember(name);
	if (it != v.MemberEnd() && it->value.IsArray()) {
		rapidjson::GenericArray<true, JsonValue> arr = it->value.GetArray();
		for (JsonValue::ConstValueIterator v = arr.Begin();v != arr.End();++v) {
			if (v->IsString()) {
				strs.push_back(v->GetString());
			}
		}
	}
}

void JsonUtils::loadInt(const JsonValue& v, const Char* name, int& i)
{
	const JsonValue::ConstMemberIterator& it = v.FindMember(name);
	if (it != v.MemberEnd() && it->value.IsInt()) {
		i = it->value.GetInt();
	}
}

void JsonUtils::loadFloat(const JsonValue& v, const Char* name, float& f)
{
	const JsonValue::ConstMemberIterator& it = v.FindMember(name);
	if (it != v.MemberEnd() && it->value.IsFloat()) {
		f = it->value.GetFloat();
	}
}

void JsonUtils::loadIntArr(const JsonValue& v, const Char* name, std::list<int>& list)
{
	const JsonValue::ConstMemberIterator& it = v.FindMember(name);
	if (it != v.MemberEnd() && it->value.IsArray()) {
		rapidjson::GenericArray<true, JsonValue> arr = it->value.GetArray();
		for (JsonValue::ConstValueIterator v = arr.Begin();v != arr.End();++v) {
			if (v->IsInt()) {
				list.push_back(v->GetInt());
			}
		}
	}
}

void JsonUtils::loadLong(const JsonValue& v, const Char* name, uint64_t& i)
{
	const JsonValue::ConstMemberIterator& it = v.FindMember(name);
	if (it != v.MemberEnd() && it->value.IsInt()) {
		i = it->value.GetInt64();
	}
}

void JsonUtils::loadLong(const JsonValue& v, const Char* name, int64_t& i)
{
	const JsonValue::ConstMemberIterator& it = v.FindMember(name);
	if (it != v.MemberEnd() && it->value.IsInt()) {
		i = it->value.GetInt64();
	}
}

String JsonUtils::jsonStr(const JsonValue& v)
{
	JsonStringBuffer stringBuffer;
	rapidjson::Writer<JsonStringBuffer,rapidjson::UTF16<>, rapidjson::UTF16<>> writer(stringBuffer);
	v.Accept(writer);
	return stringBuffer.GetString();
}

void JsonUtils::addString(JsonValue& v, const Char* name, const String& theV, JsonDoc::AllocatorType& allocator)
{
	if (!name || StringLen(name) == 0|| theV.empty()) {
		return;
	}
	JsonValue k(rapidjson::kStringType), vv(rapidjson::kStringType);
	k.SetString(name, StringLen(name), allocator);
	vv.SetString(theV.c_str(), theV.length(), allocator);
	v.AddMember(k, vv, allocator);
}

void JsonUtils::addStringArr(JsonValue& v, const Char* name, const std::list<String>& arr,
	JsonDoc::AllocatorType& allocator)
{
	if (!name || StringLen(name) == 0 || arr.empty()) {
		return;
	}
	JsonValue k(rapidjson::kStringType), vv(rapidjson::kArrayType);
	k.SetString(name, StringLen(name), allocator);
	for (std::list<String>::const_iterator it = arr.begin();it != arr.end();++it) {
		JsonValue thev(rapidjson::kStringType);
		thev.SetString(it->c_str(), allocator);
		vv.PushBack(thev, allocator);
	}
	v.AddMember(k, vv, allocator);
}

void JsonUtils::addIntArr(JsonValue& v, const Char* name, const std::list<int>& arr,
	JsonDoc::AllocatorType& allocator)
{
	if (!name || StringLen(name) == 0 || arr.empty()) {
		return;
	}
	JsonValue k(rapidjson::kStringType), vv(rapidjson::kArrayType);
	k.SetString(name, StringLen(name), allocator);
	for (std::list<int>::const_iterator it = arr.begin();it != arr.end();++it) {
		vv.PushBack(*it, allocator);
	}
	v.AddMember(k, vv, allocator);
}
